package architecture.view;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.MenuBar;
import java.awt.RenderingHints;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import java.awt.event.WindowListener;
import java.awt.event.WindowStateListener;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.*;

import javax.imageio.ImageIO;
import javax.swing.*;

import sf.jsonloader.*;
import sf.model.*;
import sf.view.*;

import architecture.dbinterface.*;
import architecture.users.User;


public class MainForm
{
	// Swing components
	private JFrame _mainForm;
	private JPanel _contentPane;
	private JMenuBar _menuBar;
	private JMenu _menuFile, _menuEdit, _menuTables, _menuReports;
	private JMenuItem _menuItemLogIn;	
	private JMenuItem _menuItemLogOut;

	private JMenu _menuPonuda, _menuKorpa, _menuProizvodi, _menuMenadzment;
	



	
	private LoginDialog loginDialog;
	private LinkedList<JMenuItem> tables, reports;
	
	// Event components
	private WindowAdapter _windowAdapter;
	private MenuActionAdapter _menuActionAdapter;
	
	private BufferedImage splashImage;
	private boolean menuActive = false;
	
	DatabaseInterface database;
	
	private User user;
	
	public MainForm()
	{
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		 database = new FullDatabase();
		
		_windowAdapter = new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
				System.exit(0); // za sada			
			}
		};
		
		
		_menuActionAdapter = new MenuActionAdapter(this);
		
		user = new User("guest",database.getRightsForUser("guest"));
		
		try	
		{
			splashImage = ImageIO.read(new File("ebroker.gif"));
			
		} catch (Exception e){
			e.printStackTrace();
		}
		
		
		_mainForm = new JFrame("E-Broker");

		loginDialog = new LoginDialog(this,true);
		
		_mainForm.setSize(550, 470);
		_mainForm.setLocationRelativeTo(null);
		_mainForm.setVisible(true);
		
		
		// bar
		initMenu();
		
		_mainForm.addWindowListener(_windowAdapter);
		
		Runnable runer = new Runnable(){
			public void run(){
				while(true){
					Graphics2D g = (Graphics2D)_mainForm.getContentPane().getGraphics();
					int xPos = _mainForm.getContentPane().getWidth() - splashImage.getWidth();
					int yPos = _mainForm.getContentPane().getHeight() - splashImage.getHeight();
					if (user.userName.equals("guest"))
						g.drawImage(splashImage, xPos/2, yPos/2, null);
					else {
						g.setFont(new Font(Font.SERIF,Font.PLAIN,20));
						g.setColor(Color.WHITE);
						g.fillRect(0, 0, _mainForm.getWidth(), _mainForm.getHeight());
						g.setColor(Color.BLACK);
						g.drawString("Dobrodosli ",10, 30);
						g.setColor(Color.BLUE);
						g.drawString(user.userName,115, 30);
					}
					try
					{
						Thread.sleep(100);
					} catch (InterruptedException e)
					{
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					g.dispose();
				}
				
			}
			
		};
		Thread splashPainter = new Thread(runer);
		splashPainter.start();
		
		
	}

	public void initMenu(){
		_menuBar = new JMenuBar();
		JPopupMenu.setDefaultLightWeightPopupEnabled(false);
		_menuFile = new JMenu("File");

		_menuFile.add(_menuItemLogIn = createMenuItem("Log in", KeyEvent.VK_L));
		_menuFile.add(_menuItemLogOut = createMenuItem("Log out", KeyEvent.VK_O));
		_menuFile.addSeparator();
		_menuFile.add(createMenuItem("Exit", KeyEvent.VK_E));
		
		_menuBar.add(_menuFile);
		
		_menuEdit = new JMenu("Edit");
		
		_menuEdit.add(createMenuItem("Settings", KeyEvent.VK_S));		
		
		_menuBar.add(_menuEdit);
		
		
		createMenus();
		
		reviewMenus(user);
	
		_mainForm.setJMenuBar(_menuBar);

	}
	
	
	public void logInAsUser(String username, String password)
	{
		if (database.getPassForUser(username).equals(password)){
			Vector<String> rights = database.getRightsForUser(username);
			//System.out.println("-------------PRAVA:" +rights);
			user = new User(username, rights);
			_mainForm.getContentPane().getGraphics().clearRect(0, 0, _mainForm.getWidth(), _mainForm.getHeight());
		}
		reviewMenus(user);
	}
	
	public void logInAsGuest() {
		user = new User("guest", database.getRightsForUser("guest"));
		reviewMenus(user);
	}
	
	public JMenuItem createMenuItem(String name, int mnemonic)
	{
		JMenuItem _tempItem = new JMenuItem(name, mnemonic);
		_tempItem.addActionListener(_menuActionAdapter);
		
		return _tempItem;
	}
	
	public void createMenus()
	{
		_menuTables = new JMenu("Tabele");
		_menuBar.add(_menuTables);
		
		tables = new LinkedList<JMenuItem>();
		
		for (String table : database.getAllTables())
		{
			tables.add(createMenuItem("Tabela " + table.toLowerCase(), 0));
			_menuTables.add(tables.getLast());
		}		

		_menuReports = new JMenu("Izvestaji");
		_menuBar.add(_menuReports);
		
		reports = new LinkedList<JMenuItem>();
		
		for (String table : database.getAllTables())
		{
			reports.add(createMenuItem("Izvestaj " + table.toLowerCase(), 0));
			_menuReports.add(reports.getLast());
		}		

		
//		_menuReports.add(reportDrzave = createMenuItem("Report drzave", KeyEvent.VK_D)); 
//		_menuReports.add(reportDrzave = createMenuItem("Report korisnicke grupe", KeyEvent.VK_G));
		
		_menuPonuda = new JMenu("Ponuda");
		_menuPonuda.add(createMenuItem("Roba i usluge", 0));
		_menuPonuda.add(createMenuItem("Cenovnik", 0));
		_menuPonuda.add(createMenuItem("Kursna lista", 0));
		_menuBar.add(_menuPonuda);
		
		_menuKorpa = new JMenu("Korpa");
		_menuKorpa.add(createMenuItem("Otvori korpu", 0));
		_menuKorpa.add(createMenuItem("Stampaj korpu", 0));
		_menuBar.add(_menuKorpa);

		_menuProizvodi = new JMenu("Proizvodi");
		_menuProizvodi.add(createMenuItem("Cene po jedinici mere", 0));
		_menuProizvodi.add(createMenuItem("Cenovnik", 0));
		_menuProizvodi.add(createMenuItem("Robe i usluge", 0));
		_menuBar.add(_menuProizvodi);
		
		_menuMenadzment = new JMenu("Menadzment");
		_menuMenadzment.add(createMenuItem("Poslovna godina", 0));
		_menuMenadzment.add(createMenuItem("Poslovni odnos", 0));
		_menuMenadzment.add(createMenuItem("Poslovne transakcije", 0));
		_menuMenadzment.add(createMenuItem("Sektori", 0));
		_menuBar.add(_menuMenadzment);
				
		
		try {
			JsonLoader.getInstance().load("exported.json");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void reviewMenus(User user)
	{				
		_menuItemLogOut.setEnabled(!user.userName.equals("guest"));
		_menuItemLogIn.setEnabled(user.userName.equals("guest"));
		
		_menuTables.setVisible(user.haveRights("MENU_TABLES"));
							
		_menuReports.setVisible(user.haveRights("MENU_REPORTS"));
		
		_menuPonuda.setVisible(user.haveRights("MENU_PONUDA"));		//16
		_menuKorpa.setVisible(user.haveRights("MENU_KORPA"));		//17
		_menuProizvodi.setVisible(user.haveRights("MENU_PROIZVODI"));	//18	
		_menuMenadzment.setVisible(user.haveRights("MENU_MENADZMENT"));	//19
		
	}	
	
	public LoginDialog getLoginDialog()
	{
		return loginDialog;
	}
	
	public JFrame getMainForm()
	{
		return _mainForm;
	}
	
	public DatabaseInterface getDatabase()
	{
		return database;
	}
	
	public static void main(String[] args)
	{
		new MainForm();
	}
	
	public void paintSplashImage(){
		
	}
	
	public User getUser()
	{
		return user;
	}
}
